package common.dao.Impl;

import assistant.domain.Evaluate;
import cn.hutool.core.util.StrUtil;
import common.dao.TeacherDao;
import common.domain.Teacher;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/1/11 10:38
 * @Author lihu
 */
public class TeacherDaoImpl implements TeacherDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(Teacher teacher) {
        String sql = "insert into teacher\n" +
                "        (teacher_id ,teacher_name, user_id, protitle_id, major_id, is_charge) \n" +
                "values(?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, teacher.getTeacherId());
            psmt.setString(2, teacher.getTeacherName());
            psmt.setLong(3, teacher.getUserId());
            psmt.setLong(4, teacher.getProtitleId());
            psmt.setLong(5, teacher.getMajorId());
            psmt.setInt(6, teacher.getIsCharge());

            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void updateById(Teacher teacher) {
        /*    String sql = "insert into teacher\n" +
            "        (teacher_id ,teacher_name, user_id, protitle_id, major_id, is_charge) \n" +
            "values(?,?,?,?,?,?,?,?)";*/
        String sql = "update teacher set";
        List<Object> param = new ArrayList<>();
        if (teacher == null) {
            return;
        } else {
            if (teacher.getTeacherName() != null) {
                sql += " teacher_name= ?,";
                param.add(teacher.getTeacherName());
            }
            if (teacher.getUserId() != null) {
                sql += " user_id = ?,";
                param.add(teacher.getUserId());
            }
            if (teacher.getProtitleId() != null) {
                sql += " `protitle_id` = ?,";
                param.add(teacher.getProtitleId());
            }
            if (teacher.getMajorId() != null) {
                sql += " major_id = ?,";
                param.add(teacher.getMajorId());
            }
            if (teacher.getIsCharge() != null) {
                sql += " is_charge = ?,";
                param.add(teacher.getIsCharge());
            }
        }
        sql = StrUtil.strip(sql, null, ",");
        sql += " where teacher_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            psmt.setLong(param.size() + 1, teacher.getTeacherId());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from teacher where teacher_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public Teacher selectById(Long id) {
        /*    String sql = "insert into teacher\n" +
            "        (teacher_id ,teacher_name, user_id, protitle_id, major_id, is_charge) \n" +
            "values(?,?,?,?,?,?,?,?)";*/
        String sql = "select * from teacher where teacher_id = ?";
        Teacher teacher = new Teacher();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            rs = psmt.executeQuery();
            while (rs.next()) {
                teacher.setTeacherId(rs.getLong(1));
                teacher.setTeacherName(rs.getString(2));
                teacher.setUserId(rs.getLong(3));
                teacher.setProtitleId(rs.getLong(4));
                teacher.setMajorId(rs.getLong(5));
                teacher.setIsCharge(rs.getInt(6));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return teacher;
    }

    @Override
    public List<Teacher> selectList(Teacher teacher) {
        String sql = "select * from teacher where 1=1";
        List<Teacher> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (teacher == null) {
            sql = "select * from teacher";
        } else {
            if (teacher.getTeacherId() != null) {
                sql += " and teacher_id = ?";
                param.add(teacher.getTeacherId());
            }
            if (teacher.getTeacherName() != null) {
                sql += " and teacher_name = ?";
                param.add(teacher.getTeacherName());
            }
            if (teacher.getUserId() != null) {
                sql += " and user_id = ?";
                param.add(teacher.getUserId());
            }
            if (teacher.getProtitleId() != null) {
                sql += " and `protitle_id` = ?";
                param.add(teacher.getProtitleId());
            }
            if (teacher.getMajorId() != null) {
                sql += " and major_id = ?";
                param.add(teacher.getMajorId());
            }
            if (teacher.getIsCharge() != null) {
                sql += " and is_charge = ?";
                param.add(teacher.getIsCharge());
            }

        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()) {
                Teacher teacher1 = new Teacher();
                teacher1.setTeacherId(rs.getLong(1));
                teacher1.setTeacherName(rs.getString(2));
                teacher1.setUserId(rs.getLong(3));
                teacher1.setProtitleId(rs.getLong(4));
                teacher1.setMajorId(rs.getLong(5));
                teacher1.setIsCharge(rs.getInt(6));
                list.add(teacher1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return list;
    }
}
